package com.acwing.partition9;

import java.io.*;

/**
 * @author rkc
 * @date 2022/9/18 16:53
 */
public class AC837连通块中点的数量 {

    private static final int N = 100010;
    private static int[] parent = new int[N], cnts = new int[N];
    private static int n, m;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]); m = Integer.parseInt(ss[1]);
        for (int i = 1; i <= n; i++) {
            parent[i] = i;
            cnts[i] = 1;
        }
        while (m-- > 0) {
            ss = reader.readLine().split(" ");
            if ("C".equals(ss[0])) {
                int a = Integer.parseInt(ss[1]), b = Integer.parseInt(ss[2]);
                a = find(a);
                b = find(b);
                if (a != b) {
                    parent[a] = b;
                    cnts[b] += cnts[a];
                }
            } else if ("Q1".equals(ss[0])) {
                int a = Integer.parseInt(ss[1]), b = Integer.parseInt(ss[2]);
                writer.write(find(a) == find(b) ? "Yes\n" : "No\n");
            } else {
                int a = Integer.parseInt(ss[1]);
                writer.write(cnts[find(a)] + "\n");
            }
        }
        writer.flush();
    }

    private static int find(int x) {
        if (x != parent[x]) parent[x] = find(parent[x]);
        return parent[x];
    }
}
